
package GUIs;

import Configs.Config;
import Controllers.Encode;
import Controllers.TaiKhoanController_2;
import javax.swing.JOptionPane;
import models.Quyen;
import models.TaiKhoan;

public class f_taikhoan_sua extends javax.swing.JFrame {

    private TaiKhoan tk;
    private Config conn;
    private TaiKhoanController_2 tkc;
    private static f_taikhoan_sua _instance;
    private f_taikhoan f_tk;
    public f_taikhoan_sua() {
        initComponents();
    }
    public f_taikhoan_sua(Config conn,TaiKhoan tk, f_taikhoan f_tk) {
        initComponents();
        this.f_tk = f_tk;
        this.conn = conn;
        this.tkc = new TaiKhoanController_2(this.conn);
        this.tk = tkc.getWithTenTaiKhoan(tk.getTaikhoan());
        tftaikhoan.setText(this.tk.getTaikhoan());
        tfmatkhau.setText(Encode.decode_url_v3(this.tk.getMatkhau()));
        tftennhanvien.setText(this.tk.getMSNV());
        if(this.tk.getIsdelete()==false)
              rbtncon.doClick();
        else
              rbtnxoa.doClick();
        if(this.tk.getQuyen().equals(Quyen.Quản_trị)){
            cbquyen.setSelectedIndex(0); 
        }
        else
            cbquyen.setSelectedIndex(1);
    }
    
     public f_taikhoan_sua(Config conn,TaiKhoan tk) {
        initComponents();
        this.conn = conn;
        this.tkc = new TaiKhoanController_2(this.conn);
        this.tk = tkc.getWithTenTaiKhoan(tk.getTaikhoan());
        tftaikhoan.setText(this.tk.getTaikhoan());
        tfmatkhau.setText(Encode.decode_url_v3(this.tk.getMatkhau()));
        tftennhanvien.setText(this.tk.getMSNV());
        if(this.tk.getIsdelete()==false)
              rbtncon.doClick();
        else
              rbtnxoa.doClick();
        if(this.tk.getQuyen().equals(Quyen.Quản_trị)){
            cbquyen.setSelectedIndex(0); 
        }
        else
            cbquyen.setSelectedIndex(1);
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        pnchinh = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        tftaikhoan = new javax.swing.JTextField();
        tfmatkhau = new javax.swing.JTextField();
        tftennhanvien = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        rbtncon = new javax.swing.JRadioButton();
        rbtnxoa = new javax.swing.JRadioButton();
        btnluu = new javax.swing.JButton();
        btnlamlai = new javax.swing.JButton();
        btnthoat = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        cbquyen = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Sửa thông tin tài khoản");
        setResizable(false);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText("SỬA THÔNG TIN TÀI KHOẢN");

        jLabel1.setText("Tên Tài Khoản : ");

        jLabel2.setText("Mật Khẩu :");

        jLabel3.setText("Của Nhân Viên : ");

        tftaikhoan.setEditable(false);
        tftaikhoan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tftaikhoanActionPerformed(evt);
            }
        });
        tftaikhoan.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tftaikhoanKeyPressed(evt);
            }
        });

        tfmatkhau.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfmatkhauKeyPressed(evt);
            }
        });

        tftennhanvien.setEditable(false);
        tftennhanvien.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tftennhanvienKeyPressed(evt);
            }
        });

        jLabel4.setText("Tình Trạng : ");

        buttonGroup1.add(rbtncon);
        rbtncon.setText("Đang Sử Dung");
        rbtncon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnconActionPerformed(evt);
            }
        });

        buttonGroup1.add(rbtnxoa);
        rbtnxoa.setSelected(true);
        rbtnxoa.setText("Đã Xóa");
        rbtnxoa.setEnabled(false);
        rbtnxoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnxoaActionPerformed(evt);
            }
        });

        btnluu.setText("Lưu ");
        btnluu.setEnabled(false);
        btnluu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnluuActionPerformed(evt);
            }
        });

        btnlamlai.setText("Làm Lại");
        btnlamlai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnlamlaiActionPerformed(evt);
            }
        });

        btnthoat.setText("Thoát");
        btnthoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnthoatActionPerformed(evt);
            }
        });

        jLabel5.setText("Quyền : ");

        cbquyen.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Quản Trị", "Nhân Viên" }));
        cbquyen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbquyenActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnchinhLayout = new javax.swing.GroupLayout(pnchinh);
        pnchinh.setLayout(pnchinhLayout);
        pnchinhLayout.setHorizontalGroup(
            pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnchinhLayout.createSequentialGroup()
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 386, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(pnchinhLayout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbquyen, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pnchinhLayout.createSequentialGroup()
                        .addComponent(rbtncon)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rbtnxoa)))
                .addGap(61, 61, 61))
            .addGroup(pnchinhLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnluu, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnlamlai, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnthoat, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnchinhLayout.createSequentialGroup()
                    .addGap(28, 28, 28)
                    .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel3))
                    .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(pnchinhLayout.createSequentialGroup()
                            .addGap(59, 59, 59)
                            .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(tftaikhoan)
                                .addComponent(tfmatkhau, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnchinhLayout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(tftennhanvien, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(64, Short.MAX_VALUE)))
        );
        pnchinhLayout.setVerticalGroup(
            pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnchinhLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 142, Short.MAX_VALUE)
                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(rbtncon)
                    .addComponent(rbtnxoa))
                .addGap(18, 18, 18)
                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbquyen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(30, 30, 30)
                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnthoat)
                    .addComponent(btnlamlai)
                    .addComponent(btnluu))
                .addGap(35, 35, 35))
            .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnchinhLayout.createSequentialGroup()
                    .addGap(59, 59, 59)
                    .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(tftaikhoan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(tfmatkhau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(tftennhanvien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(169, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(pnchinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnchinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void tftaikhoanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tftaikhoanActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tftaikhoanActionPerformed

    private void tftaikhoanKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tftaikhoanKeyPressed
        btnluu.setEnabled(true);
    }//GEN-LAST:event_tftaikhoanKeyPressed

    private void tfmatkhauKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfmatkhauKeyPressed
       btnluu.setEnabled(true);
    }//GEN-LAST:event_tfmatkhauKeyPressed

    private void tftennhanvienKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tftennhanvienKeyPressed
        btnluu.setEnabled(true);
    }//GEN-LAST:event_tftennhanvienKeyPressed

    private void btnluuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnluuActionPerformed
            String check = "";//kiem tra xem tai khoan nay co dang muon phuc hoi ko
            if(tfmatkhau.getText().equals("")){
                JOptionPane.showMessageDialog(this, "Vui lòng xem lại thông tin", "Cảnh báo", JOptionPane.WARNING_MESSAGE);
            }
            TaiKhoan tk_cu  = tkc.getWithTenTaiKhoan(tftaikhoan.getText());
            TaiKhoan tk_moi = new TaiKhoan(tftaikhoan.getText(), tfmatkhau.getText(),(cbquyen.getSelectedItem().toString().equals("Quản Trị"))?Quyen.Quản_trị:Quyen.Nhân_viên , tftennhanvien.getText());
            if(tk_cu.getIsdelete() != tk_moi.getIsdelete()){
                check = "phuchoi";
            }
            if(rbtncon.isSelected()){
                tk_moi.setIsdelete(false);
            }
            else
                tk_moi.setIsdelete(true);
            tkc.updateTaiKhoan(tk_moi,check);    
            this.f_tk.getMyFormat().clearTable(this.f_tk.getTbtk());
            tkc.showAllTaiKhoan(this.f_tk.getTbtk());
            this.setVisible(false);
            
    }//GEN-LAST:event_btnluuActionPerformed

    private void btnlamlaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnlamlaiActionPerformed
            tfmatkhau.setText("");
            cbquyen.setSelectedIndex(0);
            if(this.tk.getIsdelete()==false)
              rbtncon.doClick();
        else
              rbtnxoa.enable(true);
              rbtnxoa.doClick();
              rbtnxoa.enable(false);
    }//GEN-LAST:event_btnlamlaiActionPerformed

    private void btnthoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnthoatActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnthoatActionPerformed

    private void rbtnconActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnconActionPerformed
       btnluu.setEnabled(true);
    }//GEN-LAST:event_rbtnconActionPerformed

    private void rbtnxoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnxoaActionPerformed
        btnluu.setEnabled(true);
    }//GEN-LAST:event_rbtnxoaActionPerformed

    private void cbquyenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbquyenActionPerformed
       btnluu.setEnabled(true);
    }//GEN-LAST:event_cbquyenActionPerformed

    /**
     * @param args the command line arguments
     */
 

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnlamlai;
    private javax.swing.JButton btnluu;
    private javax.swing.JButton btnthoat;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cbquyen;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel pnchinh;
    private javax.swing.JRadioButton rbtncon;
    private javax.swing.JRadioButton rbtnxoa;
    private javax.swing.JTextField tfmatkhau;
    private javax.swing.JTextField tftaikhoan;
    private javax.swing.JTextField tftennhanvien;
    // End of variables declaration//GEN-END:variables
}
